package com.kk.permission.view;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.Html;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kk.R;
import com.kk.permission.adapter.PermissionStepAdapter;

import java.util.List;

public class PermissionStepFloatView extends FloatViewBase {
    private static final String TAG = "PermissionStepFloatView";

    private Button mSetButton;
    private TextView mTitle;
    private RecyclerView mRecyclerView;
    private PermissionStepAdapter mPermissionStepAdapter;

    private List<String> mPermissionList;

    private ValueAnimator mProgressAnim;

    public PermissionStepFloatView(Context context, List<String> permissionList) {
        super(context);
        mPermissionList = permissionList;
        initView();
    }

    @Override
    public void create() {
        super.create();

        mViewMode = FULLSCREEN_TOUCHABLE;
        mHandler = new Handler(Looper.getMainLooper());
    }

    private void initView() {
        inflate(R.layout.acc_layout_step_detail);

        mSetButton = findView(R.id.acc_step_set_action);
        mTitle = findView(R.id.acc_step_title);

//        findView(R.id.acc_step_back).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                remove();
//            }
//        });

        mRecyclerView = findView(R.id.acc_step_recycler);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        mPermissionStepAdapter = new PermissionStepAdapter(mContext, mPermissionList);
        mRecyclerView.setAdapter(mPermissionStepAdapter);
    }

    public void updateProgress(int progress, final boolean closeWhenFinished) {
        int currProgress = 0;

        if (mProgressAnim != null) {
            currProgress = (int) mProgressAnim.getAnimatedValue();
            mProgressAnim.cancel();
            mProgressAnim = null;
        }

        if (progress == currProgress) {
            mSetButton.setText(mContext.getString(R.string.acc_step_detail_set_action_fixing, progress));
            mSetButton.setBackgroundResource(R.drawable.acc_layout_step_detail_btn_fixing_bg);
            mSetButton.setEnabled(false);
            return;
        }

        mProgressAnim = ValueAnimator.ofInt(currProgress, progress);

        mProgressAnim.setDuration(1000);
        mProgressAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                int prog = (int)animation.getAnimatedValue();
                mSetButton.setText(mContext.getString(R.string.acc_step_detail_set_action_fixing, prog));
                mTitle.setText(Html.fromHtml(mContext.getString(R.string.acc_step_detail_title_fixing, prog)));
            }
        });
        mProgressAnim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                if (closeWhenFinished)
                    remove();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        mProgressAnim.start();

        mSetButton.setBackgroundResource(R.drawable.acc_layout_step_detail_btn_fixing_bg);
        mSetButton.setEnabled(false);

    }

    public void updateStepDone(int index) {
        mPermissionStepAdapter.setStatusDone(index);
        mPermissionStepAdapter.notifyItemChanged(index, PermissionStepAdapter.UPDATE_STATUS);
    }
}
